package dormouse.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.NamespaceContext;

/**
 * This class is used to provide Namespace specific parsing to an XML parser.
 * 
 * @author mfortner
 * 
 */
public class NameSpaceContextImpl implements NamespaceContext {

	private Map<String, String> map;

	/**
	 * Constructor
	 */
	public NameSpaceContextImpl() {

		map = new HashMap<String, String>();

	}

	public void setNamespace(String prefix, String namespaceURI) {

		map.put(prefix, namespaceURI);

	}

	public String getNamespaceURI(String prefix) {

		return (String) map.get(prefix);

	}

	public String getPrefix(String namespaceURI) {

		Set<String> keys = map.keySet();

		for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();)

		{

			String prefix = (String) iterator.next();

			String uri = (String) map.get(prefix);

			if (uri.equals(namespaceURI))
				return prefix;

		}

		return null;

	}

	public Iterator<String> getPrefixes(String namespaceURI) {

		List<String> prefixes = new ArrayList<String>();

		Set<String> keys = map.keySet();

		for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();)

		{

			String prefix = (String) iterator.next();

			String uri = (String) map.get(prefix);

			if (uri.equals(namespaceURI))
				prefixes.add(prefix);

		}

		return prefixes.iterator();

	}

}
